package com.createidea.cms.struts.action;

import java.util.List;

import com.createidea.cms.service.user.UserService;
import com.createidea.cms.to.UserTo;
import com.createidea.cms.utils.Pager;

public class UserAction extends BaseAction {
    private UserService userService;
    private List<UserTo> users;
    private UserTo user;
    private String userId;
    
	public String maintainusers(){
		this.pager=new Pager();
		users = userService.getUsersByStatus(1);
		if(users != null && users.size() > 0) {
			this.pager.setTotalNum(users.size());
		} else {
			this.pager.setTotalNum(0);
		}
		if(currentPage>=pager.getPageNums().size())
			currentPage=pager.getPageNums().size()-1;
	   	if(currentPage<=0)
	   		currentPage=0;
	   	pager.setCurrentPage(currentPage);
	   	users = userService.getUsersByStatusForPager(pager,1);
		return SUCCESS;
		
	}
    
	
	public String addUser(){
		return SUCCESS;
	}
	
	public String saveUser(){
		userService.saveUser(user);
		return SUCCESS;
	}
	
	public String editUser(){
		user=userService.getUserById(userId);
		return SUCCESS;
	}
	
	public String deleteUser(){
		userService.deleteUserById(userId);
		return SUCCESS;
	}
	
	public String updateUser(){
		userService.updateUser(user);
		return SUCCESS;
	}
	
	public String loadLogin(){
		return SUCCESS;
	}
	
	public String userLogin(){
		UserTo userForDb=userService.getUserByName(user.getName());
		if(userForDb==null||(userForDb!=null&&!userForDb.getPassword().equals(user.getPassword()))){
			this.addActionError("The user does not exist or the password is wrong");
		   return INPUT;
		}
		
		else {
		   this.getSession().setAttribute(this.CURRENTUSER, userForDb);
		   return SUCCESS;
		}
		
	}
	/*
	 * get and set methods
	 */
	public UserService getUserService() {
		return userService;
	}

	public void setUserService(UserService userService) {
		this.userService = userService;
	}

	public List<UserTo> getUsers() {
		return users;
	}

	public void setUsers(List<UserTo> users) {
		this.users = users;
	}


	public UserTo getUser() {
		return user;
	}


	public void setUser(UserTo user) {
		this.user = user;
	}


	public String getUserId() {
		return userId;
	}


	public void setUserId(String userId) {
		this.userId = userId;
	}
	
	
}
